package com.botann.projects.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.botann.projects.base.BaseController;
import com.botann.projects.base.Model;
import com.botann.projects.constants.API;
import com.botann.projects.utils.alioss.AliOSSUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * projects
 * AliOSSController
 * Created by 刘山国 on 2020/5/13.
 * Copyright (c) 2017 伯坦科技. All rights reserved
 */
@RestController
public class AliOSSController extends BaseController {

    @Override
    public Class loggerClass() {
        return AliOSSController.class;
    }


    @RequestMapping(value = API.URL_ALI_OSS_STS, method = RequestMethod.POST)
    public Model sts() {
        try {

//            DefaultProfile.addEndpoint("","","Sts",AliOSSUtils.sts_endpoint);
//            IClientProfile profile = DefaultProfile.getProfile("",AliOSSUtils.accessKeyId,AliOSSUtils.accessKeySecret);
//            DefaultAcsClient client = new DefaultAcsClient(profile);
//            final AssumeRoleRequest request = new AssumeRoleRequest();
//            request.setMethod(MethodType.POST);
//            request.setRoleArn(AliOSSUtils.roleArn);
//            request.setRoleSessionName(AliOSSUtils.roleSessionName);
//            request.setPolicy(AliOSSUtils.policy);
//            request.setDurationSeconds(1000L);
//            final AssumeRoleResponse response = client.getAcsResponse(request);
//
//            Map map = new HashMap<>();
//            map.put("expiration",response.getCredentials().getExpiration());
//            map.put("accessKeyId",response.getCredentials().getAccessKeyId());
//            map.put("accessKeySecret",response.getCredentials().getAccessKeySecret());
//            map.put("securityToken",response.getCredentials().getSecurityToken());
//            map.put("requestId",response.getRequestId());
//
//            return successModel(map);

            return successModel();
//            OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
//            GeneratePresignedUrlRequest res = new GeneratePresignedUrlRequest(bucketName, objectName);
//            // 设置URL过期时间为1小时。
//            Date expiration = new Date(new Date().getTime() + 3600 * 1000);
//            res.setExpiration(expiration);
//            // 设置ContentType。
////        res.setContentType(DEFAULT_OBJECT_CONTENT_TYPE);
//            // 设置自定义元信息。
//            res.addUserMetadata("author", "aliy");
//
//            // 生成PUT方式的签名URL。
//            URL signedUrl = ossClient.generatePresignedUrl(res);
//            return successModel(signedUrl.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return failureModel(e);
        }
    }
}
